Skip to content

Fix formatting of code longer than Discord's message limit#549

Merged
danthe1st merged 12 commits into
Java-Discord:mainfrom
Neil-Tomar:fix/format-code-long-messages
Jun 25, 2026
Merged

Fix formatting of code longer than Discord's message limit#549
danthe1st merged 12 commits into
Java-Discord:mainfrom
Neil-Tomar:fix/format-code-long-messages

Conversation

@Neil-Tomar

Copy link
Copy Markdown
Contributor

Description of the Changes

The "Format Code" / "Format and Indent Code" message commands and the /format-code slash command failed when the target message was longer than Discord's 2000-character limit: the whole snippet was wrapped in a single code block and sent as one message, which Discord rejects — so the command silently did nothing.

This PR splits the formatted output into chunks that each stay under the limit:

  • New Code class splits content into ≤1980-character chunks (breaking on newlines where possible) and renders each as a code block.
  • The commands acknowledge the interaction by replying with the full, unsplit code as an attached file, then post the chunks in order.
  • New Language enum + Language.fromString(...) maps the slash command's format option to a code-fence tag.

A follow-up PR will add the language-selection dropdown.

@Neil-Tomar Neil-Tomar requested a review from a team as a code owner June 20, 2026 15:17

@danthe1st danthe1st left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations on writing your first pull request. I commented on a few things I noticed. Please don't unnecessarily duplicate code.

Comment thread src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Code.java Outdated
@danthe1st

Copy link
Copy Markdown
Member

Fixes #537

@danthe1st danthe1st linked an issue Jun 20, 2026 that may be closed by this pull request
@Neil-Tomar Neil-Tomar requested a review from danthe1st June 23, 2026 15:30
…nto fix/format-code-long-messages

# Conflicts:
#	src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Code.java
#	src/main/java/net/discordjug/javabot/systems/user_commands/format_code/FormatCodeDispatcher.java
#	src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Language.java

@danthe1st danthe1st left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are mostly minor comments. Is there any good reason why users would want a file response in addition to the codeblocks?

Comment thread src/main/java/net/discordjug/javabot/systems/user_commands/format_code/Code.java Outdated
@Neil-Tomar Neil-Tomar requested a review from danthe1st June 24, 2026 07:59

@danthe1st danthe1st left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, your PR looks pretty good.

During testing, I noticed that it's possible to send a single message that can result in a lot of individual (and big). As an example, you can generate the content of such a message with the following code:

System.out.println("{".repeat(30)+"\n"+"a\n".repeat(1000)+"}".repeat(30));

Please add a limit to this. I don't want to have to clean that up without knowing the exact number (I think it is possible to trigger more than 500 messages with a single message that way). See the second review comment here for how to fix it.

@Neil-Tomar Neil-Tomar requested a review from danthe1st June 24, 2026 16:41
@Neil-Tomar Neil-Tomar requested a review from danthe1st June 25, 2026 00:57

@danthe1st danthe1st left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one small typo remaining, otherwise it looks good.

@Neil-Tomar Neil-Tomar requested a review from danthe1st June 25, 2026 14:54
Neil-Tomar and others added 3 commits June 25, 2026 20:25
…nto fix/format-code-long-messages

# Conflicts:
#	src/main/java/net/discordjug/javabot/systems/user_commands/format_code/FormatCodeDispatcher.java

@danthe1st danthe1st left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, I added some minor changes after testing.

@danthe1st danthe1st merged commit 9a03a81 into Java-Discord:main Jun 25, 2026
3 checks passed
@Neil-Tomar Neil-Tomar deleted the fix/format-code-long-messages branch June 27, 2026 03:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Exception formatting code >2k characters

2 participants